﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HWmanagementClient.Models;
using HWmanagementClient.HWManager;

namespace HWmanagementClient.Controllers
{
    public class MessageController : Controller
    {
        protected override void ExecuteCore()
        {
            try
            {
                ViewBag.Rank = HWmanagementClient.Codes.Site.GetAuthUserRank(Request, User);
            }
            catch
            {
                ViewBag.Rank = -1;
            }
            base.ExecuteCore();
        }


        //
        // GET: /Message/


        private UserMessages loadMessages()
        {
            UserMessages mes = new UserMessages();

            mes.userData = HWmanagementClient.Codes.Site.GetAuthUserData(Request, User);

            //TODO ez helyett DB
            ManagerClient proxy = new ManagerClient();

            mes.messages = proxy.getMessages(mes.userData.Item2).ToList().Select((mm) => MessageClientModel.fromWCF(mm)).ToList();
            foreach (var item in mes.messages)
            {
                if (item.from.ToUpper().Contains(mes.userData.Item2.ToUpper()))
                {
                    item.to = string.Format("{0} ({1})", proxy.GetLoggedInUser(item.to).Name, item.to);
                    item.isinbox = false;
                }
                else
                {
                    item.from = string.Format("{0} ({1})", proxy.GetLoggedInUser(item.from).Name, item.from);
                    item.isinbox = true;
                }
            }

            return mes;
        }

        public ActionResult Index()
        {
            return View(loadMessages());
        }

        [HttpPost]
        public ActionResult Index(UserMessages model)
        {
            var neptun = model.messagemodel.to.Split(new[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries).Last();

            //kell majd a küldés IDE //TODO
            ManagerClient proxy = new ManagerClient();
            try
            {
                proxy.sendMessage(new MessageModel()
                {
                    subject = model.messagemodel.subj,
                    messageBody = model.messagemodel.message
                    .Replace("[nl]", "<br>")
                    .Replace("[b]", "<strong>")
                    .Replace("[xb]", "</strong>")
                    .Replace("[i]", "<em>")
                    .Replace("[xi]", "</em>")
                    .Replace("[u]", "<u>")
                    .Replace("[xu]", "</u>")
                    ,
                    is_read = false,
                    receiver = neptun,
                    sender = HWmanagementClient.Codes.Site.GetAuthUserCode(Request, User)
                });
                ViewBag.Succeeded = true;
            }
            catch 
            {
                ViewBag.Succeeded = false;
            }

            return View(loadMessages());
        }

        [HttpPost]
        public ActionResult AutoComplete()
        {
            ManagerClient proxy = new ManagerClient();
            
            var model = proxy.getAllUser().Select((um) => string.Format("{0} ({1})",um.Name,um.Neptun));            
            return Json(model);
        }

        [HttpPost]
        public ActionResult Readed(int id)
        {
            ManagerClient proxy = new ManagerClient();
            proxy.setRead(id, true);

            return Json("");
        }

        

    }
}
